
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ret;
        if (root == nullptr) return ret;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty())
        {
            int size = q.size();
            int max = INT_MIN;
            while (size--)
            {
                TreeNode* cur = q.front();
                q.pop();
                max = max > cur->val ? max : cur->val;
                if (cur->left) q.push(cur->left);
                if (cur->right) q.push(cur->right);
            }
            ret.push_back(max);
        }
        return ret;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        vector<pair<TreeNode*, unsigned int>> v;
        unsigned ret = 0;
        v.push_back({ root, 0 });
        while (!v.empty())
        {
            auto& [x1, y1] = v[0];
            auto& [x2, y2] = v.back();
            ret = max(ret, (y2 - y1 + 1));
            //ret = ret > y2 - y1 + 1? ret: y2 - y1 + 1;
            vector<pair<TreeNode*, unsigned int>> num;
            for (auto& [x, y] : v)
            {
                if (x->left) num.push_back({ x->left, 2 * y });
                if (x->right) num.push_back({ x->right, 2 * y + 1 });
            }
            v = num;
        }
        return ret;
    }
};
